package com.example.demo2.controller;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.AdminDO;
import com.example.demo2.pojo.vo.CategoryVO;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.vo.AnnouncementVO;
import com.example.demo2.service.AnnouncementService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/** 
 * <br/>
 * Created by weidong on 2020/05/06
 */
@Controller
public class AnnouncementController {

	@Resource
	private AnnouncementService announcementService;



	@RequestMapping("/anno")
	public String list( @RequestParam(defaultValue = "1") Integer currentPage, Model model) throws Exception {
		AnnouncementVO announcementVO=new AnnouncementVO();
		announcementVO.setCurrentPage(currentPage);
		PageBean pageBean=announcementService.listPage(announcementVO);
		model.addAttribute("pageBean", pageBean);
		return "anno";
	}

	/**
	* 删除
	*/
	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id)throws Exception{
		return announcementService.deleteById(id);
	}

	/**
	* 更新
	*/
	@RequestMapping("anno/updateById")
    @ResponseBody
	public Result updateById( String title,String content)throws Exception{
		AnnouncementVO announcementVO=new AnnouncementVO();
		announcementVO.setId(1);
		announcementVO.setTitle(title);
		announcementVO.setContent(content);
		AdminDO principal = (AdminDO) SecurityUtils.getSubject().getPrincipal();
		announcementVO.setWriter(principal.getUsername());
		Date date = new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		announcementVO.setPublishTime(format.format(date));
		return announcementService.updateById(announcementVO);
	}

	/**
	* 根据ID查询
	*/
	@RequestMapping("/anno/findById")
	@ResponseBody
	public Result findById()throws Exception{
		return announcementService.findById(1);
	}

	/**
	* 查询
	*/
//	@RequestMapping("/list")
//	@ResponseBody
//	public Result list(AnnouncementVO announcement)throws Exception{
//		return announcementService.list(announcement);
//	}

}